/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormBarangGol.java
 *
 * Created on Feb 4, 2011, 1:17:06 AM
 */

package juwita;

import config.DBConnection;
import java.sql.*;
//import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Abah
 */
public class FormBarangGol extends javax.swing.JInternalFrame {

    private DefaultTableModel DftTabMode;
    java.sql.Connection conn = new DBConnection().connect();

    /** Creates new form FormBarangGol */
    public FormBarangGol() {
        initComponents();
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);

        tampilData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnTengah = new javax.swing.JPanel();
        spBarangGol = new javax.swing.JScrollPane();
        tbBarangGol = new javax.swing.JTable();
        pnAtas = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        txtNamaGol = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtKdGol = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtKet = new javax.swing.JTextArea();
        btnShowAll = new javax.swing.JButton();
        txtNamaSearch = new javax.swing.JTextField();
        btnNew = new javax.swing.JButton();
        cbSearch = new javax.swing.JComboBox();
        btnSearch = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Master Golongan Barang");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        pnTengah.setBackground(new java.awt.Color(255, 255, 255));
        pnTengah.setLayout(new java.awt.BorderLayout());

        tbBarangGol.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbBarangGol.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbBarangGolMousePressed(evt);
            }
        });
        tbBarangGol.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbBarangGolKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbBarangGolKeyReleased(evt);
            }
        });
        spBarangGol.setViewportView(tbBarangGol);

        pnTengah.add(spBarangGol, java.awt.BorderLayout.CENTER);

        pnAtas.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas.setPreferredSize(new java.awt.Dimension(410, 200));
        pnAtas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        pnAtas.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 130, 70, 20));

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        pnAtas.add(btnEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 100, 70, -1));
        pnAtas.add(txtNamaGol, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 40, 320, -1));

        jLabel2.setText("Nama Gol");
        pnAtas.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        txtKdGol.setEditable(false);
        txtKdGol.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtKdGolKeyReleased(evt);
            }
        });
        pnAtas.add(txtKdGol, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 61, -1));

        jLabel3.setText("Kode Gol");
        pnAtas.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        jLabel4.setText("Keterangan");
        pnAtas.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));

        txtKet.setColumns(20);
        txtKet.setRows(5);
        jScrollPane1.setViewportView(txtKet);

        pnAtas.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 70, 220, 57));

        btnShowAll.setText("Show All");
        btnShowAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllActionPerformed(evt);
            }
        });
        pnAtas.add(btnShowAll, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 170, 80, -1));
        pnAtas.add(txtNamaSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 140, 190, -1));

        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });
        pnAtas.add(btnNew, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 70, 70, -1));

        cbSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "kode gol", "nama gol" }));
        pnAtas.add(cbSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, 90, -1));

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        pnAtas.add(btnSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 170, 70, -1));

        pnTengah.add(pnAtas, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(pnTengah, java.awt.BorderLayout.CENTER);

        setBounds(0, 0, 432, 360);
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        int clossing;
        clossing = JOptionPane.showConfirmDialog(this,
                "Tutup Master Golongan Barang?",
                "Konfirmasi",
                JOptionPane.YES_NO_OPTION);
        if (clossing == 0){
            this.dispose();
            FormMain.isfBrgGol = false;
        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
        tampilTabelSearch();
        txtKdGol.setText("");
        txtKdGol.requestFocus();
        txtNamaGol.setText("");
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
}//GEN-LAST:event_btnSearchActionPerformed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        // TODO add your handling code here:
        kosongkanNew();
}//GEN-LAST:event_btnNewActionPerformed

    private void btnShowAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowAllActionPerformed
        // TODO add your handling code here:
        tampilData();
        txtNamaSearch.setText("");
}//GEN-LAST:event_btnShowAllActionPerformed

    private void txtKdGolKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtKdGolKeyReleased
        // TODO add your handling code here:
        txtKdGol.setText(txtKdGol.getText().toUpperCase());
}//GEN-LAST:event_txtKdGolKeyReleased

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        //BARU KOPAS
        if(btnEdit.getText().equals("Save")){
            if(txtKdGol.getText().equals("") || txtNamaGol.getText().equals("") ) {
                JOptionPane.showMessageDialog(this, "Kode golongan dan nama golongan harus diisi!",
                        "Informasi",JOptionPane.INFORMATION_MESSAGE);
            }else{
                if(isDuplicatePKey()==true){
                    kosongkanNew();
                    txtKdGol.requestFocus();
                }else{
                    insertData();
                }
            }
        }else if (btnEdit.getText().equals("Edit")){
            editData();
        }

        //*
        //*/
}//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if(btnDelete.getText().equals("Cancel")){
            kosongkan();
            tampilData();
        }else if(btnDelete.getText().equals("Delete")){
            deleteData();
        }

        //*
        //*/
}//GEN-LAST:event_btnDeleteActionPerformed

    private void tbBarangGolKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBarangGolKeyReleased
        // TODO add your handling code here:
        getDataFromTable();
}//GEN-LAST:event_tbBarangGolKeyReleased

    private void tbBarangGolKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBarangGolKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tbBarangGolKeyPressed

    private void tbBarangGolMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbBarangGolMousePressed
        getDataFromTable();
}//GEN-LAST:event_tbBarangGolMousePressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnShowAll;
    private javax.swing.JComboBox cbSearch;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnAtas;
    private javax.swing.JPanel pnTengah;
    private javax.swing.JScrollPane spBarangGol;
    private javax.swing.JTable tbBarangGol;
    private javax.swing.JTextField txtKdGol;
    private javax.swing.JTextArea txtKet;
    private javax.swing.JTextField txtNamaGol;
    private javax.swing.JTextField txtNamaSearch;
    // End of variables declaration//GEN-END:variables

    private void kosongkan(){
        txtKdGol.setEditable(false);
        txtKdGol.setText("");
        txtKdGol.requestFocus();
        txtNamaGol.setText("");
        txtKet.setText("");
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        btnEdit.setText("Edit");
        btnDelete.setText("Delete");                    
    }
    private void kosongkanNew(){
        txtKdGol.setEditable(true);
        txtKdGol.setText("");
        txtNamaGol.setText("");
        txtKet.setText("");
        txtNamaSearch.setText("");
        btnEdit.setText("Save");
        btnDelete.setText("Cancel");
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
    }
    private void tampilData(){
        Object[] Baris={"No","Kode","Nama"};
        DftTabMode=new DefaultTableModel(null,Baris);
        tbBarangGol.setModel(DftTabMode);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol");
            while (rslt.next()) {
                int no = rslt.getRow();
                String sKdGol = rslt.getString("kdGol");
                String sNamaGol = rslt.getString("nmGol");
                //String sKet = rslt.getString("ket");
                //String sTest = rslt.getString("test");

                String[] dataField = {Integer.toString(no), sKdGol, sNamaGol};//, sKet};
                DftTabMode.addRow(dataField);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void tampilTabelSearch(){
        /*
        String[] columnNames =
        {"No","Kode","Kota"};

        table = new  JTable(
                getDataSearch(), columnNames);
        spKota.setViewportView(table);
        */
        String srcby = "";
        if(cbSearch.getSelectedItem().toString().equals("kode gol")){
            srcby = "kdGol";
        }else{
            srcby = "nmGol";
        }
        
        Object[] Baris={"No","Kode","Nama"};
        DftTabMode=new DefaultTableModel(null,Baris);
        tbBarangGol.setModel(DftTabMode);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbrg_gol WHERE UPPER(" + srcby +
                    ") like UPPER('%" +
                    txtNamaSearch.getText() + "%')");
            //int iNo = 0;
            while (rslt.next()) {
                int x = rslt.getRow();
                String sKdGol = rslt.getString("kdGol");
                String sNamaGol = rslt.getString("nmGol");
                //String sNamaKota = rslt.getString("ket");

                String[] dataField = {Integer.toString(x), sKdGol, sNamaGol};
                DftTabMode.addRow(dataField);
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }
    private void deleteData(){
            try {
                int deleting;
                deleting = JOptionPane.showConfirmDialog(this,
                        "Apakah Anda yakin akan menghapus "

                        //+ cbKodeKota.getSelectedItem().toString() +" "
                        +txtKdGol.getText() +"?",
                        "Konfirmasi",
                        JOptionPane.YES_NO_OPTION);
                if (deleting == 0){

                    PreparedStatement pStatement = null;
                    String sql = "DELETE FROM tbrg_gol " +
                            "WHERE kdGol=? ";
                    pStatement = conn.prepareStatement(sql);

                    pStatement.setString(1, txtKdGol.getText());

                    int intTambah = pStatement.executeUpdate();
                    if (intTambah >0){
                        JOptionPane.showMessageDialog(this,
                                "Delete sukses", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);

                    }else {
                        JOptionPane.showMessageDialog(this,
                                "Delete gagal", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);
                    }

                    pStatement.close();

                    kosongkan();
                    tampilData();
                }
            } catch(SQLException e){
                System.out.println("koneksi gagal " + e.toString());
            }
    }
    private boolean isDuplicatePKey(){
        //Object oKdGol, oNamaGol;//, oKet;//, oTest;
        String pKey = txtKdGol.getText();
        boolean result = true;
        int row = 0;

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol WHERE kdGol=" +
                    "'" +pKey+ "'");
            while (rslt.next()) {
                row = rslt.getRow();                
            }
                if(row > 0){
                    JOptionPane.showMessageDialog(this, "Kode "+pKey+" sudah ada"
                            ,"Peringatan",JOptionPane.WARNING_MESSAGE);
                    result = true;
                }else{
                    result = false;
                }
            System.out.println("row : " + row);
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        return result;
    }
    private void insertData(){
        // panggil method koneksi
        //java.sql.Connection conn = new DBConnection().connect();

        java.sql.PreparedStatement pStatement = null;
        String sql = "INSERT INTO tbrg_gol " +
                "(kdGol, nmGol, ket) " +
                "VALUES (?,?,?)";
        try {
            pStatement = conn.prepareStatement(sql);
            pStatement.setString(1, txtKdGol.getText());
            pStatement.setString(2, txtNamaGol.getText());
            //String kdKota = (String)cbKodeKota.getSelectedItem();
            //pStatement.setString(2, kdKota);
            pStatement.setString(3, txtKet.getText());

            int intTambah = pStatement.executeUpdate();
            if (intTambah>0){
                JOptionPane.showMessageDialog(this,
                        "Penambahan sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this,
                        "Penambahan gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            pStatement.close();
            //conn.close();

            kosongkan();
            tampilData();
            
        }
        catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }

    }

     private void editData(){
         //nomor ieu mah
        //String sKdGol = tbBarangGol.getValueAt(
               // tbBarangGol.getSelectedRow(), 0).toString();

        try {
            PreparedStatement pStatement = null;
            String sql = "UPDATE tbrg_gol " +
                    "SET nmGol=?, ket=? " +
                    "WHERE kdGol=? ";
            pStatement = conn.prepareStatement(sql);

            //pStatement.setString(1, cbKodeKota.getSelectedItem().toString());
            //pStatement.setString(1, txtKdGol.getText());
            pStatement.setString(1, txtNamaGol.getText());
            pStatement.setString(2, txtKet.getText());
            pStatement.setString(3, txtKdGol.getText());

            int intTambah = pStatement.executeUpdate();
            if (intTambah >0){
                JOptionPane.showMessageDialog(this,
                        "Edit sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);

            }else {
                JOptionPane.showMessageDialog(this,
                        "Edit gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }

            //tbBarangGol.get
            pStatement.close();
            kosongkan();
            tampilData();

            
        } catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }
    }
    private void getDataFromTable(){
                // TODO add your handling code here:
        Object oKdGol, oNamaGol;//, oKet;//, oTest;

        oKdGol = tbBarangGol.getValueAt(tbBarangGol.getSelectedRow(), 1);
        oNamaGol = tbBarangGol.getValueAt(tbBarangGol.getSelectedRow(), 2);
        //oKet = tbBarangGol.getValueAt(tbBarangGol.getSelectedRow(), 2);
        //oTest = tbKota.getValueAt(tbKota.getSelectedRow(), 3);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbrg_gol WHERE kdGol=" +
                    "'" +oKdGol+ "'");
            while (rslt.next()) {
                txtKet.setText(rslt.getString("ket"));
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        /*
        if(oTest == null){
            oTest = " ";
        }
         *
         */

        txtKdGol.setText(oKdGol.toString());
        //cbKodeKota.setSelectedItem(oKdKota);
        txtNamaGol.setText(oNamaGol.toString());
        //txtKet.setText(oKet.toString());
        //txtTest.setText(oTest.toString());

        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
    }
}
